﻿
using DataWorks_IBLL;
using DataWorks_Sqlite.MappingClass;
using DataWorks_Tools.MappModals;
using DataWorks_Tools.ProcessExcute.Clean;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using System.Reflection;
using System.Text;


namespace DataWorks_WebApi.Controllers
{
    [EnableCors("react")]
    [Route("[controller]/[action]")]
    [ApiController]
    public class DW_FeishuController : ControllerBase
    {
        private readonly ILogger<DW_FeishuController> _logger;
        private readonly string webhook= "https://open.feishu.cn/open-apis/bot/v2/hook/8c747f10-baf1-4263-8624-2b148e17caf5";
       
        public DW_FeishuController(ILogger<DW_FeishuController> logger)
        {
            _logger = logger;
        }

        /// <summary>
        /// 飞书发送机器人信息
        /// </summary>
        /// <param name="loginname">用户名</param>
        /// <returns></returns>
      
        [HttpPost]
        public async Task<IActionResult> SendFeishuMessage(string messageText)
        {
            var message = new
            {
                msg_type = "text",
                content = new
                {
                    text = "边缘计算器提醒："+messageText+ "<at user_id=\"1g88g9b5\">邱宇</at>"
                }
            };
            using (var client = new HttpClient())
            {
                var content = new StringContent(System.Text.Json.JsonSerializer.Serialize(message), Encoding.UTF8, "application/json");

                var response = await client.PostAsync(webhook, content);

                if (response.IsSuccessStatusCode)
                {
                    var responseContent = await response.Content.ReadAsStringAsync();
                    _logger.LogInformation($"飞书发送信息{messageText}成功：{DateTime.Now}");
                    return Ok(responseContent);
                }
                else
                {
                    _logger.LogInformation($"飞书发送信息{messageText}失败{response.StatusCode}：{DateTime.Now}");
                    return BadRequest("Error: " + response.StatusCode);
                }
            }
        }
       
    }
}
